<!DOCTYPE html>
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" /><!-- bootstrap -->
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script> <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <script type="text/javascript" src="/layer/layer.js"></script><!-- layer -->
    <script type="text/javascript" src="/js/md5.min.js"></script><!-- md5.js -->
    <script type="text/javascript" src="/js/common.js"></script><!-- common.js -->
    <script type="text/javascript" src="/js/socket.js"></script><!-- common.js -->
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
    <div id="userTip" style="display: none">
        <span> 您还没有登录，请<a href="/login.html">登陆</a>后再操作<br/></span>
    </div>
    <span>没有收货地址的提示。。。</span>
</div>
<table class="table">
    <tr>
        <td>商品名称</td>
        <td colspan="3" id="goodName"></td>
    </tr>
    <tr>
        <td>商品图片</td>
        <td colspan="3"><img id="goodImg"  width="200" height="200" /></td>
    </tr>
    <tr>
        <td>秒杀开始时间</td>
        <td id="startDate"></td>
        <td id="seckillTip"></td>
        <td>
            <img id="verifyCodeImg" width="80" height="32"  onclick="initVerifyCodeImg()" style="display: none">
            <input id="verifyCode" style="display: none">
            <button class="btn btn-primary btn-block" type="button" id="buyButton" onclick="doGetPath()" disabled="disabled">立即秒杀</button>
        </td>
    </tr>
    <tr>
        <td>商品原价</td>
        <td colspan="3" id="goodPrice"></td>
    </tr>
    <tr>
        <td>秒杀价</td>
        <td colspan="3" id="seckillPrice"></td>
    </tr>
    <tr>
        <td>库存数量</td>
        <td colspan="3" id="stockCount"></td>
    </tr>
</table>
</div>
<script>
    var seckillGoodId;
    $(function(){
        seckillGoodId = getQueryString("goodId");
        initGood();
        initUser();
    })
    function initGood(){
        $.ajax({
            url:'http://localhost:9000/seckill/seckillGood/find?seckillGoodId='+seckillGoodId,
            type:"GET",
            xhrFields: { withCredentials: true },//withCredentials: true表示在发送跨域请求的时候需要把cookie信息带上.
            success:function(data){
                if(data.code==200){
                    render(data.data);
                    renderDate(data.data.startDate,data.data.endDate);
                }else{
                    layer.msg(data.msg);
                }
            }
        });
    }
    function render(seckillGood){
        $("#goodName").html(seckillGood.goodName);
        $("#goodImg").prop("src",seckillGood.goodImg);
        $("#startDate").html(seckillGood.startDate);
        $("#goodPrice").html(seckillGood.goodPrice);
        $("#seckillPrice").html(seckillGood.seckillPrice);
        $("#stockCount").html(seckillGood.stockCount);
    }
    var remainStartSeconds = 0;//距离开始时间还有多少秒
    var remainEndSeconds = 0;//距离结束时间还有多少秒
    var timer;
    function renderDate(sDate,eDate){
        var startDate = new Date(sDate); // 2019-8-8 17:00
        var endDate = new Date(eDate);  // 2019-8-8 23:00
        var now = new Date(); // 2019-8-8 16:30
        remainStartSeconds = parseInt((startDate.getTime() -  now.getTime())/1000);
        remainEndSeconds = parseInt((endDate.getTime() -  now.getTime())/1000);
        show();
        timer = window.setInterval(show,1000);
    }
    function show(){
        // remainStartSeconds > 0 ===> 说明秒杀还没开始
        // remainStartSeconds <=0 && remainEndSeconds> 0 说明秒杀中
        // remainStartSeconds <=0  && remainEndSeconds <= 0 秒杀已经结束了
        remainStartSeconds--;
        remainEndSeconds--;
        if(remainStartSeconds > 0){
            $("#seckillTip").html("秒杀倒计时:"+remainStartSeconds+"秒");
        }else if(remainEndSeconds> 0){
            $("#buyButton").prop("disabled",false);
            $("#seckillTip").html("秒杀中");
        }else{
            $("#buyButton").prop("disabled",true);
            window.clearInterval(timer);
            $("#seckillTip").html("秒杀已经结束");
        }
    }
    function initUser(){
        $.ajax({
            url:'http://localhost:9000/member/token/getCurrent',
            type:"GET",
            xhrFields: { withCredentials: true },//withCredentials: true表示在发送跨域请求的时候需要把cookie信息带上.
            success:function(data){
                if(data.code==200){
                    renderUser(data.data);
                }else{
                    layer.msg(data.msg);
                }
            }
        });
    }
    var user;
    var uuid;
    function renderUser(u){
        user = u;
        if(!u){
            $("#userTip").show();
        }else{
            uuid = getUuid(u.id);
            $("#verifyCodeImg").show();
            $("#verifyCode").show();
            initVerifyCodeImg();
        }
    }
    function doSeckill(path){
        var socket;
        $.ajax({
            url:'http://localhost:9000/seckill/order/'+path+'/seckill',
            type:"POST",
            data:{seckillGoodId:seckillGoodId,uuid:uuid},
            xhrFields: { withCredentials: true },//withCredentials: true表示在发送跨域请求的时候需要把cookie信息带上.
            success:function(data){
                if(data.code==200){
                    layer.msg("下单操作成功,请耐心等待下单结果.");
                    showLoading();
                    if(!socket){
                        socket = createScoket(uuid);
                    }
                }else{
                    layer.msg(data.msg);
                }
            }
        });
    }
    function doGetPath(){
        if(!user){
            layer.msg("您没有登录,请先登录.");
            return;
        }
        $.ajax({
            url:'http://localhost:9000/seckill/getPath',
            type:"GET",
            data:{uuid:uuid,result:$("#verifyCode").val()},
            xhrFields: { withCredentials: true },//withCredentials: true表示在发送跨域请求的时候需要把cookie信息带上.
            success:function(data){
                if(data.code == 200){
                    doSeckill(data.data);
                }else{
                    layer.msg(data.msg);
                }
            }
        });
    }
    function initVerifyCodeImg(){
        $("#verifyCodeImg").prop("src","http://localhost:9000/seckill/getVerifyCodeImage?uuid="+uuid+"&timestamp="+new Date().getTime());
    }
</script>
</body>
</html>